package yuejian_07.yuejian_07.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.jude.rollviewpager.RollPagerView;

import java.util.List;

import yuejian_07.yuejian_07.R;
import yuejian_07.yuejian_07.RetrofitListener;
import yuejian_07.yuejian_07.activity.TrainCaijiActivity;
import yuejian_07.yuejian_07.activity.TrainGaoshouActivity;
import yuejian_07.yuejian_07.activity.TrainJinjieActivity;
import yuejian_07.yuejian_07.activity.TrainSearchActivity;
import yuejian_07.yuejian_07.adapter.TrainListAdapter;
import yuejian_07.yuejian_07.javabean.Train;
import yuejian_07.yuejian_07.model.TrainModel;

public class TrainFragment extends Fragment implements RetrofitListener<List<Train>> {

    private RecyclerView recyclerView;
    private List mDataList;
    private View view=null;
    private RollPagerView rollPV = null;
    private Button caiji,jinjie,gaoshou;
    private ImageView search;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view=inflater.inflate(R.layout.tab01_train, container, false);
        initViews();
        initEvents();
        getTrainlist();
        return view;
    }

    private void initViews(){
        recyclerView= (RecyclerView)view.findViewById(R.id.rv_train);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        caiji=(Button) view.findViewById(R.id.button1);
        jinjie=(Button) view.findViewById(R.id.button2);
        gaoshou=(Button) view.findViewById(R.id.button3);
        search=(ImageView) view.findViewById(R.id.search);
    }

    private void initEvents(){


        /*入门跳转*/
        caiji.setOnClickListener(new View.OnClickListener(){//购买跳转
            @Override
            public void onClick(View v){
                Intent intent = new Intent(TrainFragment.this.getActivity(), TrainCaijiActivity.class);
                startActivity(intent);
            }
        });
        jinjie.setOnClickListener(new View.OnClickListener(){//购买跳转
            @Override
            public void onClick(View v){
                Intent intent = new Intent(TrainFragment.this.getActivity(), TrainJinjieActivity.class);
                startActivity(intent);
            }
        });
        gaoshou.setOnClickListener(new View.OnClickListener(){//购买跳转
            @Override
            public void onClick(View v){
                Intent intent = new Intent(TrainFragment.this.getActivity(), TrainGaoshouActivity.class);
                startActivity(intent);
            }
        });
        search.setOnClickListener(new View.OnClickListener(){//购买跳转
            @Override
            public void onClick(View v){
                Intent intent = new Intent(TrainFragment.this.getActivity(), TrainSearchActivity.class);
                startActivity(intent);
            }
        });



    }




    public void getTrainlist(){
        TrainModel trainModel=new TrainModel();
        trainModel.trainlist(this);
    }

    @Override
    public void onSuccess(List<Train> data){

        //System.out.printf("data");
        recyclerView.setAdapter(new TrainListAdapter(TrainFragment.this.getActivity(),data));
    }

    @Override
    public void onFail(){ Toast.makeText(getActivity(), "网络发生错误", Toast.LENGTH_SHORT).show();}

}